package com.jerry.shop.controller;

import com.jerry.shop.bean.Category;
import com.jerry.shop.bean.Product;
import com.jerry.shop.exception.SellException;
import com.jerry.shop.form.ProductForm;
import com.jerry.shop.service.CategoryService;
import com.jerry.shop.service.ProductService;
import com.jerry.shop.utils.KeyUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.validation.Valid;
import java.util.List;
import java.util.Map;

/**
 * 卖家端商品
 */
@Controller
@RequestMapping("/seller/product")
public class SellerProductController {

    @Autowired
    private ProductService productService;
    @Autowired
    private CategoryService categoryService;

    //列表
    @GetMapping("/list")
    public ModelAndView list(@RequestParam(value = "page", defaultValue = "1") Integer page,
                              @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                              Map<String, Object> map) {
        List<Product> productAllList = productService.findAll();
        int pageNum = productAllList.size() / pageSize;
        if (productAllList.size() % pageSize != 0) {
            pageNum = pageNum + 1;
        }
        List<Product> productList = productService.findAllByPage(page, pageSize);
        map.put("productList", productList);
        map.put("pageNum", pageNum);
        map.put("currentPage", page);
        map.put("pageSize", pageSize);
        return new ModelAndView("product/list", map);
    }

    @GetMapping("/addOrUpdateInfo")
    public ModelAndView addOrUpdateInfo(@RequestParam(value = "id", required = false) String id,
                                   Map<String, Object> map) {
        if (!StringUtils.isEmpty(id)) {
            Product product = productService.findOne(id);
            map.put("product", product);
        }

        //查询所有的类目
        List<Category> categoryList = categoryService.findAll();
        map.put("categoryList", categoryList);

        return new ModelAndView("product/addOrUpdate", map);
    }

    //保存/更新
    @PostMapping("/addOrUpdate")
    public ModelAndView addOrUpdate(@Valid ProductForm form,
                             BindingResult bindingResult,
                             Map<String, Object> map) {
        //绑定错误提示
        if (bindingResult.hasErrors()) {
            map.put("msg", bindingResult.getFieldError().getDefaultMessage());
            map.put("url", "/seller/product/addOrUpdateInfo");
            return new ModelAndView("common/error", map);
        }

        Product product = new Product();
        try {
            BeanUtils.copyProperties(form, product);
            productService.saveOrUpdate(product);
        } catch (SellException e) {
            map.put("msg", e.getMessage());
            map.put("url", "/seller/product/addOrUpdateInfo");
            return new ModelAndView("common/error", map);
        }

        map.put("url", "/seller/product/list");
        return new ModelAndView("common/success", map);
    }

    //商品上架
    @RequestMapping("/onSale")
    public ModelAndView onSale(@RequestParam("id") String id,
                               Map<String, Object> map) {
        try {
            productService.onSale(id);
        } catch (SellException e) {
            map.put("msg", e.getMessage());
            map.put("url", "/seller/product/list");
            return new ModelAndView("common/error", map);
        }

        map.put("url", "/seller/product/list");
        return new ModelAndView("common/success", map);
    }

    //商品下架
    @RequestMapping("/offSale")
    public ModelAndView offSale(@RequestParam("id") String id,
                               Map<String, Object> map) {
        try {
            productService.offSale(id);
        } catch (SellException e) {
            map.put("msg", e.getMessage());
            map.put("url", "/seller/product/list");
            return new ModelAndView("common/error", map);
        }

        map.put("url", "/seller/product/list");
        return new ModelAndView("common/success", map);
    }

}
